import pandas as pd #importing all the important packages
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

import streamlit as st

plt.style.use('fivethirtyeight')



def pre_data():
    df = pd.read_csv('data/Pokemon.csv') #read the csv file and save it into a variable
    df.columns = df.columns.str.upper().str.replace('_', '') #change into upper case
    df = df.set_index('NAME')
    df.index = df.index.str.replace(".*(?=Mega\s*)", "", regex=True)
    df=df.drop(['#'],axis=1)
    df[df['TYPE 2'].isna()]
    df['TYPE 2'].fillna(df['TYPE 1'], inplace=True)
    return df

df= pre_data()

def pk_table():
    # df = pre_data()
    
    st.dataframe(df)
       
    islegendary =st.checkbox("是否为传奇宝可梦")
    
    df2 = df[df['LEGENDARY']==islegendary].head(5) 
    
    st.dataframe(df2)
    
def plot_demo1():
    # df = pre_data()
    bins=range(0,200,40) #they act as containers
    plt.hist(df["ATTACK"],bins,histtype="bar",rwidth=1.2,color='#0ff0ff') #hist() is used to plot a histogram
    plt.xlabel('Attack') #set the xlabel name
    plt.ylabel('Count') #set the ylabel name   
    plt.axvline(df['ATTACK'].mean(),linestyle='dashed',color='red') #draw a vertical line showing the average 
    
    st.pyplot(plt)
    
    
def plot_demo2():
    mypk1 = st.selectbox("选择宝可梦的类型1：",df['TYPE 1'].unique())
    mypk2 = st.selectbox("选择宝可梦的类型2：",df['TYPE 1'].unique())
    
    fire=df[(df['TYPE 1']==mypk1) | ((df['TYPE 2'])==mypk1)] #fire contains all fire pokemons
    water=df[(df['TYPE 1']==mypk2) | ((df['TYPE 2'])==mypk2)] #all water pokemins
    plt.scatter(fire.ATTACK.head(50),fire.DEFENSE.head(50),color='Red',label=mypk1,marker="*",s=50) #scatter 
    plt.scatter(water.ATTACK.head(50),water.DEFENSE.head(50),color='Blue',label=mypk2,s=25)
    plt.xlabel("Attack")
    plt.ylabel("DEFENCE")
    plt.legend()
    # plt.plot()
    fig=plt.gcf() #get the current figure using .gcf()
    fig.set_size_inches(12,6) #set the size for the figure
    # plt.show()
    st.pyplot(plt)
    
    